// pages/login/login.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    syWidth: "",//系统宽度
    syHeight: "",//系统高度
    bgWW:"",//图片宽度
    bgHH: "",//图片高度
    inputValue:"",//输入值保存
    cishu:0,//签到人数
    booltrue:false,
    boolname:true,
    name:"李玲",
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this
    //获取设备宽高
    wx.getSystemInfo({
      success: function (res) {
        that.setData({
          syWidth: res.windowWidth,
          syHeight: res.windowHeight,
          bgWW: res.windowWidth,
          bgHH: (res.windowWidth*1057)/750,
        })
      }
    })
    console.log("设备宽：" + that.data.syWidth + "|设备高：" + that.data.syHeight)
    that.selectcishu();//查询
  },
  //输入值获取
  bindKeyInput:function(e){
    this.setData({
      inputValue: e.detail.value
    })
  },
  //开启参赛模式
  cansaiOnClick:function(){
    var that=this;
    if (that.data.inputValue!=""){
      that.checkuserforzhanghao(that.data.inputValue);//查询该帐号
    
    }else{
      that.messhow("请输入姓名！")
    }
  }
  ,
  //校验用户，根据帐号mode
  checkuserforzhanghao: function (_mode) {

    var that = this
    wx.request({
      url: 'https://www.citytk.com/citytk/drai/modeht/mode.php',
      header: {
        "Content-Type": "application/x-www-form-urlencoded"
      },
      data: {
        mode: "checkuserforzhanghao",
        zhanghao: _mode
      },
      method: "POST",
      success: function (res) {
        console.log("获取校验zhanghao数据：" + res.data)
        if (res.data != "") {

          if (res.data[0].cishu>0){
            that.messhow("签到成功！")
            that.upedatecishu(res.data[0].name)
            that.setData({
              name: res.data[0].name,
              booltrue: true,
              boolname: false,
            })
          }else{
            that.setData({
              name: res.data[0].name,
              booltrue: true,
              boolname: false,
            })
            that.messhow("您已签到！")
          }
         

        } else {
          that.messhow("姓名不在名单内，请主动联系场地管理员核实！")
          console.log("数据库没有此zhanghao，请重新输入")
        }
      },
      fail: function (e) {
        that.messhow("数据连接失败。。。联系管理员");
        console.log("数据联系失败e:" + e.errMsg)
      }
    })

  },
  //更新用户次数
  upedatecishu:function(_name){

    var that = this
    wx.request({
      url: 'https://www.citytk.com/citytk/drai/modeht/mode.php',
      header: {
        "Content-Type": "application/x-www-form-urlencoded"
      },
      data: {
        mode: "updateusercishu",
        nickname: _name,
      },
      method: "POST",
      success: function (res) {
        console.log("更新次数结果：" + res.data)
        if (res.data != "") {
          console.log(res.data)
          that.selectcishu();
        } else {
          that.messhow("更新失败，请重新输入或者联系管理员！")
          console.log("更新次数失败")
        }
      },
      fail: function (e) {
        that.messhow("数据连接失败。。。联系管理员");
        console.log("数据联系失败e:" + e.errMsg)
      }
    })
  },
  //查询签到次数
  selectcishu:function(){
    var that = this
    wx.request({
      url: 'https://www.citytk.com/citytk/drai/modeht/mode.php',
      header: {
        "Content-Type": "application/x-www-form-urlencoded"
      },
      data: {
        mode: "sumcishu",
      },
      method: "POST",
      success: function (res) {
        console.log("签到查询结果：" + res.data)
        if (res.data != "") {
          console.log(res.data[0].tol)
          var _cishu = 305-res.data[0].tol;
          that.setData({
            cishu:_cishu,
          })
        } else {
          that.messhow("更新失败，请重新输入或者联系管理员！")
          console.log("更新次数失败")
        }
      },
      fail: function (e) {
        that.messhow("数据连接失败。。。联系管理员");
        console.log("数据联系失败e:" + e.errMsg)
      }
    })
  },
  //开启练习模式
  lianxiOnClick: function () {
    wx.reLaunch({
      url: '../txtedit/txtedit?mode=lianxi',
    })
  },
  //提示器
  messhow: function (str) {
    wx.showToast({
      title: str,
      icon: 'none',
      duration: 3000
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})